<?
	require("err.class.php");
	require("request.class.php");
	if(!defined('sslib_settings_path'))	define('sslib_settings_path', 'private/.settings');
	Sslib::Init(sslib_settings_path);
//	setlocale(LC_ALL, array ('ru_RU.CP1251', 'rus_RUS.1251')); 
	
	date_default_timezone_set('Europe/Moscow');
	define('SSLIB_Sslib', TRUE);
	
	require("html.class.php");
	require("template.class.php");
	require("utils.php");
	require('session.class.php');
	require('group.class.php');
	require('page.class.php'); //Must define Page class before session_start calling.
	
	$g_sslib_templates = array();
	
	/**
	 * Main SSLib class
	 *
	 */
	class Sslib
	{
		static function Init($settings)
		{
			Sslib::LoadSettings($settings);
			Sslib::CompleteSettings();
			Sslib::LoadLocalizedMessages(Sslib::Variable('sslib.language'));
				
			header("Content-type: text/html; charset=".Sslib::Variable('page.charset'));
			date_default_timezone_set(Sslib::Variable('site.timezone'));
			include('database.class.php');
		}
		static function LoadLocalizedMessages($lang)
		{
			$sslibroot = Sslib::Variable('sslib.path');
			if($lang)	$file = $sslibroot.'languages/'.Sslib::Variable('sslib.language').'.inc';
			else 		include ($sslibroot.'languages/en.inc');
			
			if(file_exists($file))	include $file;
			else 					include ($sslibroot.'languages/en.inc');
		}
		private static function CompleteSettings()
		{
			if(empty($_ENV['sslib_settings']['log.format.date']))
				$_ENV['sslib_settings']['log.format.date'] = 'y-m-d h:i ';
			if(empty($_ENV['sslib_settings']['site.private.path']))
				$_ENV['sslib_settings']['site.private.path'] = Sslib::CiteRoot().'private/';
			if(empty($_ENV['sslib_settings']['sslib.language']))
				$_ENV['sslib_settings']['sslib.language'] = 'en';
			if(empty($_ENV['sslib_settings']['sslib.error.ignore']))
				$_ENV['sslib_settings']['sslib.error.ignore']='';
		}
		/**
		 * Return or create Database object. Make some checks, and reuse created Database object, if needed.
		 *
		 * @return Database
		 */
		static function DB()
		{
			
			if(!array_key_exists('sslib_database', $_ENV))
			{
				//if(!defined("SSLIB_Database")) die(__FILE__." Database class missing");
				$_ENV['sslib_database'] = new Database();				
			}
			$db = $_ENV['sslib_database'];
			return $db;
		}
		/**
		 * Return root path for the cite. If $path parameter is not null then return merged path.
		 *
		 * @param string $path
		 * @return string
		 */
		static function CiteRoot()
		{
			return array_key_exists('site.path', $_ENV['sslib_settings'])?
				Sslib::Variable('site.path'):
				str_replace("\\","/",realpath(dirname(__FILE__)."/..").'/');
		}

/**
		 * Library version
		 *
		 * @return string
		 */
		public static function Version()		{return "2.0";		}
		/**
		 * Return default "users" table, where 'ownerid'->'id' column stored.
		 *
		 * @return string
		 */
		public static function TableUsers()		{return "users";	}
		public static function TableGroups()	{return "groups";	}
		public static function Debug(){return true;}
		/**
		 * Return settings value
		 *
		 * @return string $variable
		 */
		public static function Variable($name)
		{
			if(!array_key_exists($name, $_ENV['sslib_settings'])) die("Variable \"$name\" not found! Exit."); 
			return $_ENV['sslib_settings'][$name];
		}
		/**
		 * Load global settings 
		 *
		 * @return void
		 */
		private static function LoadSettings($path)
		{
			$settings = array();
			$f = fopen($path, 'r') or die('Can\'t read Sslib settings file.');
			while($val = fgets($f))
			{
				if($val[0] == '#') continue;
				$val = split('=', $val);
				$val[0] = trim($val[0]);
				
				if(is_array($val) && !empty($val[0]))
				{
					$val[1] = str_replace('"','', trim($val[1]));
					$settings[$val[0]] = $val[1];
				}
			}
			fclose($f);
			
			$_ENV['sslib_settings'] = $settings;
		}
		static function LocalizedMessage($message)
		{
			if(array_key_exists($message, $GLOBALS['sslib_localized_messages']))
				$message = $GLOBALS['sslib_localized_messages'][$message];
			else throw new Err('Missing or does\'t translated message: '.$message);
			$pagecharset = Sslib::Variable('page.charset');
			if(stripos($pagecharset, 'UTF-8') === false)
			{
				$message = iconv('UTF-8', $pagecharset, $message);
			}
			return $message;
		}
		 
	}
?>